import type { FormRule, PrimaryTableCol, SelectProps } from 'tdesign-vue-next';
import { Icon, Tag } from 'tdesign-vue-next';
import { h } from 'vue';

import { t } from '@/locales';

/**
 * 搜索表单项
 */
export const searchParams: SearchParamsProps[] = [
    {
        label: t('pages.sysPermission.name'),
        key: 'name',
    },
    {
        label: t('pages.sysPermission.englishName'),
        key: 'englishName',
    },
    {
        label: '',
        key: 'type',
        hide: true,
        defaultValue: 0,
    },
    {
        label: '',
        key: 'tree',
        hide: true,
        defaultValue: true,
    },
];

/**
 * 表格列
 */
export const columns: PrimaryTableCol[] = [
    {
        colKey: 'row-select',
        type: 'multiple',
        width: 50,
    },
    {
        title: t('pages.sysPermission.name'),
        ellipsis: true,
        align: 'center',
        colKey: 'name',
    },
    {
        title: t('pages.sysPermission.englishName'),
        ellipsis: true,
        align: 'center',
        colKey: 'englishName',
    },
    {
        title: t('pages.sysPermission.path'),
        ellipsis: true,
        align: 'center',
        colKey: 'path',
    },
    {
        title: t('pages.sysPermission.code'),
        ellipsis: true,
        align: 'center',
        colKey: 'code',
    },
    {
        title: t('pages.sysPermission.outerChain'),
        ellipsis: true,
        align: 'center',
        colKey: 'outerChain',
        cell: (h, { row }) =>
            h(
                Tag,
                { theme: row.outerChain == 0 ? 'warning' : 'success' },
                t(`components.YesNo.${row.outerChain == 0 ? 'no' : 'yes'}`),
            ),
    },
    {
        title: t('pages.sysPermission.icon'),
        ellipsis: true,
        align: 'center',
        colKey: 'icon',
        cell: (h, { row }) => h(Icon, { name: row.icon }),
    },
    {
        title: t('pages.sysPermission.sort'),
        ellipsis: true,
        align: 'center',
        colKey: 'sort',
    },
    {
        title: t('pages.sysPermission.remarks'),
        ellipsis: true,
        align: 'center',
        colKey: 'remarks',
    },
];

/**
 * 表单校验规则
 */
export const rules: Record<string, FormRule[]> = {
    type: [{ required: true, message: t('pages.sysPermission.required.type'), type: 'error', trigger: 'all' }],
    parentId: [
        { required: true, message: t('pages.sysPermission.required.parentId'), type: 'error', trigger: 'change' },
    ],
    name: [{ required: true, message: t('pages.sysPermission.required.name'), type: 'error', trigger: 'blur' }],
    englishName: [
        { required: true, message: t('pages.sysPermission.required.englishName'), type: 'error', trigger: 'blur' },
    ],
    path: [{ required: true, message: t('pages.sysPermission.required.path'), type: 'error', trigger: 'blur' }],
    sort: [{ required: true, message: t('pages.sysPermission.required.sort'), type: 'error', trigger: 'blur' }],
    code: [{ required: true, message: t('pages.sysPermission.required.code'), type: 'error', trigger: 'blur' }],
};

export const typeOptions: SelectProps['options'] = [
    {
        label: t('pages.sysPermission.typeOptions.parent'),
        value: 0,
    },
    {
        label: t('pages.sysPermission.typeOptions.children'),
        value: 1,
    },
    {
        label: t('pages.sysPermission.typeOptions.perms'),
        value: 2,
    },
];

export const yesNoOptions: SelectProps['options'] = [
    {
        label: t(`components.YesNo.no`),
        value: 0,
    },
    {
        label: t(`components.YesNo.yes`),
        value: 1,
    },
];
